<?php

namespace app\util;

use think\Db;
use think\model;

/*
 * GPT
 * 开发文档 https://apifox.com/apidoc/shared-0f112256-234e-4795-b60b-471a4988a046
 * token   Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJodWljZXBpbmciLCJpYXQiOjE2ODkwODgxODQsImV4cCI6MTc1MTI5NjE4NH0.hjyWgTl3YN3OiXQhQ4m26jEwIvhOSCw8AP8bvPyRX2s
 */

class GPT{
    
    const TOKEN = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJodWljZXBpbmciLCJpYXQiOjE2ODkwODgxODQsImV4cCI6MTc1MTI5NjE4NH0.hjyWgTl3YN3OiXQhQ4m26jEwIvhOSCw8AP8bvPyRX2s';
    
    /**
     * 发送消息
     */
    public function sendSMS($modelName, $content)
    {
        $postFields = array(
            'modelName' => $modelName,
            'content' => $content,
        );
        $postFields = json_encode($postFields);

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.hxkj.vip/aigc/gpt/message',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_POSTFIELDS => $postFields,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_HTTPHEADER => array(
               'Authorization: '.self::TOKEN,
               'User-Agent: Apifox/1.0.0 (https://apifox.com)',
               'Content-Type: application/json'
            ),
         ));

        $response = curl_exec($curl);

        curl_close($curl);
        
        $rs = json_decode ($response, 1);
        return $rs;

    }
    
    
    /**
     * 查询余量
     */
    public function usage()
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
           CURLOPT_URL => 'https://api.hxkj.vip/aigc/gpt/usage',
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_ENCODING => '',
           CURLOPT_MAXREDIRS => 10,
           CURLOPT_TIMEOUT => 0,
           CURLOPT_FOLLOWLOCATION => true,
           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
           CURLOPT_CUSTOMREQUEST => 'GET',
           CURLOPT_HTTPHEADER => array(
              'Authorization: '.self::TOKEN,
              'User-Agent: Apifox/1.0.0 (https://apifox.com)',
              'Accept:application/json, text/javascript'
           ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $rs = json_decode ($response, 1);
        return $rs;


    }
    
}
